package thread.threadPool;

import date.UtilDate;

import java.text.ParseException;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 定时调度任务的方法
 * Created by E440 on 2017/4/21.
 */
public class ScheduelThreadPoolDemo {
    public static void main(String[] args) {
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3);

        for (int i = 0; i < 6; i++) {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println(Thread.currentThread().getName() + " is bombing~~");
                }
            };

            // executorService.schedule(runnable,1, TimeUnit.SECONDS);
            // executorService.scheduleAtFixedRate(runnable, 4, 2, TimeUnit.SECONDS);//没有决定定时启动的时间,可按下面的转化
            String   dataFormatter = "yyyy-MM-dd HH:mm:ss";
           // SimpleDateFormat formatter     = new SimpleDateFormat(dataFormatter);

            try {
                Date date = UtilDate.parse("2017-4-21 17:05:00",dataFormatter);
                System.out.println(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            executorService.scheduleAtFixedRate(runnable, 4, 2, TimeUnit.SECONDS);
        }

        // executorService.shutdown();
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
